package com.example.webdemo.controller;

import com.example.webdemo.bean.orderitem;
import com.example.webdemo.dao.OrderItemDao;
import com.example.webdemo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/orderitems")
public class OrderItemController {

    @Autowired
    private OrderItemDao orderItemDao;

    // 创建订单项
    @PostMapping
    public JsonResult<orderitem> createOrderItem(@RequestBody orderitem orderItem) {
        try {
            int result = orderItemDao.createOrderItem(orderItem);
            if (result > 0) {
                return JsonResult.success(orderItem);
            } else {
                return JsonResult.error("创建订单项失败");
            }
        } catch (Exception e) {
            return JsonResult.error("创建订单项失败: " + e.getMessage());
        }
    }

    // 批量创建订单项
    @PostMapping("/batch")
    public JsonResult<orderitem> createOrderItems(@RequestBody List<orderitem> orderItems) {
        try {
            int result = orderItemDao.createOrderItems(orderItems);
            if (result > 0) {
                return JsonResult.success("成功创建 " + result + " 个订单项");
            } else {
                return JsonResult.error("创建订单项失败");
            }
        } catch (Exception e) {
            return JsonResult.error("批量创建订单项失败: " + e.getMessage());
        }
    }

    // 获取订单项详情
    @GetMapping("/{id}")
    public JsonResult<orderitem> getOrderItemById(@PathVariable int id) {
        try {
            orderitem orderItem = orderItemDao.getOrderItemById(id);
            if (orderItem == null) {
                return JsonResult.error("订单项不存在");
            }
            return JsonResult.success(orderItem);
        } catch (Exception e) {
            return JsonResult.error("获取订单项详情失败: " + e.getMessage());
        }
    }

    // 根据订单ID获取所有订单项
    @GetMapping("/order/{orderId}")
    public JsonResult<List<orderitem>> getItemsByOrderId(@PathVariable int orderId) {
        try {
            List<orderitem> items = orderItemDao.getItemsByOrderId(orderId);
            return JsonResult.success(items);
        } catch (Exception e) {
            return JsonResult.error("获取订单项列表失败: " + e.getMessage());
        }
    }

    // 更新订单项
    @PutMapping("/{id}")
    public JsonResult<orderitem> updateOrderItem(@PathVariable int id, @RequestBody orderitem orderItem) {
        try {
            orderItem.setItem_id(id);
            int result = orderItemDao.updateOrderItem(orderItem);
            if (result > 0) {
                return JsonResult.success(orderItem);
            } else {
                return JsonResult.error("更新订单项失败");
            }
        } catch (Exception e) {
            return JsonResult.error("更新订单项失败: " + e.getMessage());
        }
    }

    // 删除订单项
    @DeleteMapping("/{id}")
    public JsonResult<orderitem> deleteOrderItem(@PathVariable int id) {
        try {
            int result = orderItemDao.deleteOrderItem(id);
            if (result > 0) {
                return JsonResult.success("订单项删除成功");
            } else {
                return JsonResult.error("订单项删除失败");
            }
        } catch (Exception e) {
            return JsonResult.error("删除订单项失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/order/{orderId}")
    public JsonResult<Void> deleteItemsByOrderId(@PathVariable int orderId) {
        try {
            int result = orderItemDao.deleteItemsByOrderId(orderId);
            return JsonResult.success("删除成功");
        } catch (Exception e) {
            return JsonResult.error("删除失败: " + e.getMessage());
        }
    }
}
